# Hey Emacs, this is a -*- makefile -*-
#
# Copyright (C) 2012 The Paparazzi Team
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

#
# This is the common Makefile for the nps target.
#
SRC_ARCH = arch/sim

OPT ?= 2
# Slightly bigger .elf files but gains the ability to decode macros
DEBUG_FLAGS ?= -ggdb3

# Launch with "make Q=''" to get full command display
Q=@

# flags for warnings (C and C++)
WARN_FLAGS += -W -Wall -Wextra
WARN_FLAGS += -Wunused -Wcast-align -Wpointer-arith -Wmissing-declarations
#WARN_FLAGS += -Wcast-qual
#WARN_FLAGS += -Wredundant-decls
#WARN_FLAGS += -Wshadow

CSTANDARD ?= -std=gnu99
CFLAGS  = $(WARN_FLAGS)
CFLAGS += $(INCLUDES)
CFLAGS += $($(TARGET).CFLAGS)
CFLAGS += $(USER_CFLAGS) $(BOARD_CFLAGS)
CFLAGS += -O$(OPT) -fPIC
CFLAGS += $(DEBUG_FLAGS)
CFLAGS += $(CSTANDARD)
CFLAGS += $(shell pkg-config --cflags-only-I ivy-glib)
CFLAGS += -D_GNU_SOURCE

CXXSTANDARD ?= -std=c++17
CXXFLAGS  = $(WARN_FLAGS)
CXXFLAGS += $(INCLUDES)
CXXFLAGS += $($(TARGET).CFLAGS)
CXXFLAGS += $($(TARGET).CXXFLAGS)
CXXFLAGS += $(USER_CFLAGS) $(BOARD_CFLAGS)
CXXFLAGS += -O$(OPT)
CXXFLAGS += $(DEBUG_FLAGS)
CXXFLAGS += $(CXXSTANDARD)
CXXFLAGS += $(shell pkg-config --cflags-only-I ivy-glib)
CXXFLAGS += -D_GNU_SOURCE

LDFLAGS	+= $($(TARGET).LDFLAGS)
LDFLAGS += $(BOARD_LDFLAGS)
LDFLAGS += $(patsubst %,-L%,$(DLIBDIR) $(RUST_DIRS))
LDFLAGS += $(RUST_LIBS)

# x86/64 and armv7 allow unaligned access
CFLAGS += -DPPRZLINK_UNALIGNED_ACCESS=1

#
# General rules
#

$(TARGET).srcsnd = $(notdir $($(TARGET).srcs))
$(TARGET).objso	= $($(TARGET).srcs:%.c=$(OBJDIR)/%.o)
$(TARGET).objsoxx = $($(TARGET).objso:%.cpp=$(OBJDIR)/%.o)
$(TARGET).objs	= $($(TARGET).objsoxx:%.S=$(OBJDIR)/%.o)

all compile: check_jsbsim $(OBJDIR)/simsitl


check_jsbsim:
	@echo Paparazzi jsbsim package found: $(FIND_JSBSIM_VIA_PKG_CONFIG)


$(OBJDIR)/simsitl : $($(TARGET).objs)
	@echo LD $@
	$(Q)$(CXX) $(CXXFLAGS) -o $@ $($(TARGET).objs) $(LDFLAGS)


%.s: %.c
	$(CC) $(CFLAGS) -S -o $@ $<

%.s: %.cpp
	$(CC) $(CFLAGS) -S -o $@ $<

$(OBJDIR)/%.s: %.c
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -S -o $@ $<

$(OBJDIR)/%.s: %.cpp
	@echo CXX $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -S -o $@ $<

$(OBJDIR)/%.o: %.c $(OBJDIR)/../Makefile.ac
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -MMD $(CFLAGS) -c -o $@ $<

$(OBJDIR)/%.o: %.cpp $(OBJDIR)/../Makefile.ac
	@echo CXX $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CXX) -MMD $(CXXFLAGS) -c -o $@ $<

.PHONY: all compile check_jsbsim


#
# Dependencies
#
ifneq ($(MAKECMDGOALS),clean)
DEPS = $(addprefix $(OBJDIR)/,$($(TARGET).srcs:.c=.d))
DEPS += $(addprefix $(OBJDIR)/,$($(TARGET).srcs:.cpp=.d))
-include $(DEPS)
endif
